var express = require('express');
var router = express.Router();
var db = require('../db.js');

/* GET home page. */
router.get('/', function(req, res, next) {
	// 查询最新发布的四个商品
	var sql=" SELECT id,name,price,image,add_time FROM product order by add_time desc limit 0,4";
	db.conn.query(sql,function(err,result){
		if(err){
			console.log(err);
			return;
	  }
		console.log(result);
		//查询最新发布的五条新闻
		var sqlNews=" SELECT id,title,add_time FROM news order by add_time desc limit 0,5";
		db.conn.query(sqlNews,function(err,resultNews){
			if(err){
				console.log(err);
				return;
		}		
		res.render('index', { products:result,news:resultNews});
	});
	  
  });
  
});

//用户注册
router.get('/register', function(req, res, next) {
			res.render('register', { });
  
});

router.post('/register', function(req, res, next) {
	//获取form表单信息
	var username = req.body.username;
	var password = req.body.password;
	var realname = req.body.realname;
	var gender = req.body.gender;
	var email = req.body.email;
	var phone = req.body.phone;
	var address = req.body.address;
	//将用户信息存放到数据库
	var sql = 'insert into users(username,password,realname,gender,email,phone,address)'
	var params = [username,password,realname,gender,email,phone,address];
	db.conn.query(sql,params,function(err,result){
		if(err){
			console.log('用户注册失败'+err.message);
			
		}else{
			console.log(result);
			res.render('login',{})
		}
	})
	
});

//用户登录
router.get('/login', function(req, res, next) {
	res.render('login', { });
  
});
router.post('/login', function(req, res, next) {
	var username = req.body.username;
	var password = req.body.password;
	var sql = "select * from user where username = ? and password = ?"
	var params = [username , password];
	db.conn.query(sql, params, function(err,result){
		if(err){
			console.log("查询失败"+err.message);
			
		}else{
			if(result.toString().trim() == ""){
				res.render("login",{errMsg:"用户名或密码错误"})
			}else{
				res.redirect("/");
			}
		}
	})
} )


// 商品列表
router.get('/product', function(req, res, next) {
	var cat_id=req.query.cat_id; // 获取地址栏参数
	if(cat_id){  //分类查询
		var sql=" SELECT * FROM product where cat_id=? order by id desc";
		var sqlParam=[cat_id];
	}else{ //全表查询
		var sql=" SELECT * FROM product order by id desc";
		var sqlParam=[];		
	}
	
	db.conn.query(sql,sqlParam,function(err,result){
		if(err){
			console.log(err);
			return;
	  }
		console.log(result);
		res.render('productList', { products:result,cat_id:cat_id});
	});
  
});

// 商品详情
router.get('/detail/:id', function(req, res, next) {
	var id=req.params.id; // 获取地址栏动态参数
	var sql=" SELECT * FROM product where id=?";
	var sqlParam=[id];
	db.conn.query(sql,sqlParam,function(err,result){
		if(err){
			console.log(err);
			return;
	  }
		console.log(result);
		res.render('detail', { oneProduct:result});
	});  
});

// 图文页面
router.get('/page/:pid', function(req, res, next) {
	var pid=req.params.pid; // 获取地址栏动态参数
	var sql=" SELECT * FROM page where id=?";
	var sqlParam=[pid];
	db.conn.query(sql,sqlParam,function(err,result){
		if(err){
			console.log(err);
			return;
	  }
		console.log(result);
		res.render('page', { pageContent:result,pid:pid});
	});  
});


// 新闻列表
// 商品列表
router.get('/news', function(req, res, next) {
	var cat_id=req.query.cat_id; // 获取地址栏参数
	if(cat_id){  //分类查询
		var sql=" SELECT * FROM news where cat_id=? order by id desc";
		var sqlParam=[cat_id];
	}else{ //全表查询
		var sql=" SELECT * FROM news order by id desc";
		var sqlParam=[];		
	}
	
	db.conn.query(sql,sqlParam,function(err,result){
		if(err){
			console.log(err);
			return;
	  }
		console.log(result);
		res.render('newsList', { news:result,cat_id:cat_id});
	});
  
});
module.exports = router;
